package com.tao.power.Controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.net.URLEncodeUtil;
import cn.hutool.core.net.URLEncoder;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.tao.power.Dto.Result;
import com.tao.power.Dto.UserQueryDto;
import com.tao.power.Dto.UserRolesDto;
import com.tao.power.Pojo.Roler;
import com.tao.power.Pojo.User;
import com.tao.power.Service.UserService;
import com.tao.power.config.AuthorityAnnotation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/user")
public class UserController {


    @Resource
    private UserService userService;

    @PostMapping("/insertUser")
    @AuthorityAnnotation(value = "insertUser")
    public Result insertUser(@RequestBody User user) throws Exception {
        return userService.insertUser(user);
    }

    @RequestMapping("/queryUser")
    public Result queryUser(Integer count, Integer currentPage) {
        return userService.queryUser(count, currentPage);
    }

    @PostMapping("/updateUser")
    @AuthorityAnnotation(value = "updateUser")
    public Result updateUser(@RequestBody User user) throws Exception {
        return userService.updateUser(user);
    }

    @PostMapping("/deleteUser")
    @AuthorityAnnotation(value = "deleteUser")
    public Result deleteUser(@RequestBody Integer id){
        return userService.deleteUser(id);
    }

    @PostMapping("/queryUserBySearch")
    public Result queryUserBySearch(@RequestBody UserQueryDto userQueryDto) {
        return userService.queryUserBySearch(userQueryDto.getUsername(), userQueryDto.getName()
                , userQueryDto.getStatus(), userQueryDto.getStartTime(), userQueryDto.getEndTime()
                ,userQueryDto.getCurrentPage(),userQueryDto.getPageSize());
    }

    @PostMapping("/export")
    @AuthorityAnnotation(value = "export")
    public void export(@RequestBody List<Integer> idList,HttpServletResponse response) throws Exception {
        userService.exportUser(idList, response);
    }

    @GetMapping("/uploadMoBan")
    public void uploadMoBan(HttpServletResponse response) throws Exception {
        userService.uploadMoBan(response);
    }

    @PostMapping("/import")
    @AuthorityAnnotation(value = "import")
    public Result importUser(MultipartFile multipartFile,HttpServletResponse response) throws IOException {
        return userService.importUser(multipartFile,response);
    }

    @PostMapping("/setRoles")
    @AuthorityAnnotation(value = "userRoles")
    public Result setRoles(@RequestBody UserRolesDto roles){
        return userService.setRoles(roles);
    }

    @PostMapping("/userBatchDelete")
    @AuthorityAnnotation(value = "userBatchDelete")
    public Result batchDel(@RequestBody JSONArray idListArray){
        List<Integer> idList = JSONUtil.toList(idListArray,int.class).stream().distinct().collect(Collectors.toList());
        return userService.batchDel(idList);
    }

    @PostMapping("/queryRolerById")
    public Result queryRolerById(@RequestBody Integer id){
        return userService.queryRolerById(id);
    }

    @PostMapping("/updateStatus")
    @AuthorityAnnotation(value = "updateUser")
    public Result updateStatus(@RequestBody User user) {
       return userService.updateStatus(user);
    }
}
